package com.qifan.alb2b.action.back;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qifan.alb2b.action.back.param.UserQueryParam;
import com.qifan.alb2b.domain.CompanyInfoDomain;
import com.qifan.alb2b.domain.UserInfoDomain;
import com.qifan.alb2b.service.CompanyInfoService;
import com.qifan.alb2b.service.UserInfoService;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ID;
import com.qifan.alb2b.util.Page;
import com.qifan.alb2b.util.Stringer;

@Controller
@RequestMapping("/admin/user")
public class UserManageController{
	private UserInfoService userInfoService;
	private CompanyInfoService companyInfoService;

	public void setCompanyInfoService(CompanyInfoService companyInfoService) {
		this.companyInfoService = companyInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	@RequestMapping("/query.adm")
	@ResponseBody
	public Page query(DataGridModel dgm,UserQueryParam uqp) {
		//spring太给力了，可以自动装配两个对象  会自动的装返回的Map转换成JSON对象
		return userInfoService.query(dgm,uqp);
	}
	
	@RequestMapping("/add.adm")
	@ResponseBody
	public Map<String, Object> add(UserInfoDomain userInfoDomain){
		CompanyInfoDomain companyInfoDomain =companyInfoService.getCompByName(userInfoDomain.getCompName());
		userInfoService.getUserByUserName(userInfoDomain.getUserName());
		Map<String, Object> map=new HashMap<String, Object>();
		userInfoDomain.setUserId(ID.next()+"");//设置唯一ID
		userInfoDomain.setUserPwd(Stringer.md5(Constants.PASSWORD).substring(0,30));
		userInfoDomain.setCompId(companyInfoDomain.getCompId());//设置唯一ID
		userInfoDomain.setStatus(Constants.EXAMINE);
		if(userInfoDomain.getUserBirth()==null||userInfoDomain.getUserBirth().equals("")){
			userInfoDomain.setUserBirth(null);
		}
		userInfoDomain.setCreateTime(new Date());//设置创建日期
		userInfoService.add(userInfoDomain);
		map.put("success", true);
		return map;
	}
	
	@RequestMapping("/update.adm")
	@ResponseBody
	public Map<String, Object> update(UserInfoDomain userInfoDomain){
		UserInfoDomain userInfo = userInfoService.getUserById(userInfoDomain.getUserId());
		Map<String, Object> map=new HashMap<String, Object>();
		userInfoDomain.setCompId(ID.next()+"");//设置唯一ID
		userInfoDomain.setCreateTime(new Date());//设置创建日期
		userInfoDomain.setUserPwd(userInfo.getUserPwd());
		userInfoService.update(userInfoDomain);
		map.put("success", true);	
		return map;
	}
	
	@RequestMapping("/del.adm")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("uid") String[] ids){
		Map<String, Object> map=new HashMap<String, Object>();
		userInfoService.delete(ids);
		map.put("success", true);
		return map;
	}
	
	@RequestMapping("/updateStatus.adm")
	@ResponseBody
	public Map<String, Object> updateStatus(@RequestParam("uid") String[] ids,@RequestParam("status") String status){
		System.out.println(ids[0]);
		Map<String, Object> map=new HashMap<String, Object>();
		userInfoService.updateStatus(ids,status);
		map.put("success", true);
		return map;
	}
	
}
